<?php

class FP_Permissions extends FP_Gateway {

    protected $_dao_class  = 'FP_Dao_Permission';

    protected $_model_class = 'FP_Permission';

    protected $_primary_key = 'permission_id';

    public function fetchPermissionsByType($pType){

	return $this->fetch(array('type' => $pType));

    }

    public function fetchByResourceAndPrivilege($pResource, $pPrivilege){
	$results = $this->fetch(array('resource' => $pResource, 'privilege' => $pPrivilege));
	if (count($results) > 0)
	    return $results->current();
	return null;
    }

    public function fetchPermissionByName($pPermission){

	$results = $this->fetch(array('name' => $pPermission));

	if (count($results) > 0)
	    return $results->current();
	return null;

    }

    public function fetchPermissionsForRole($pRoleId){

	$results = Doctrine_Query::create()
	    ->from(FP_Application::getNamespace() . "_Dao_RolePermission o")
	    ->where("o.role_id = ?", $pRoleId)
	    ->execute();

	$permissions = array();

	foreach ($results as $result){
	    $permissions[] = new $this->_model_class($result->Permission->toArray(), $this);
	}


	if (count($permissions) > 0)
	    return $this->getModelCollection($permissions);
	return array();

    }

    public function clearPermissionsForRole($pRoleId){

	Doctrine_Query::create()
	    ->delete(FP_Application::getNamespace() . "_Dao_RolePermission o")
	    ->where("o.role_id = ?", $pRoleId)
	    ->execute();
    }

    public function fetchPermissionsForUser($pUserId){

	$results = Doctrine_Query::create()
	    ->from(FP_Application::getNamespace() . "_Dao_UserPermission o")
	    ->where("o.user_id = ?", $pUserId)
	    ->execute();

	$permissions = array();

	foreach ($results as $result){
	    $permissions[] = new $this->_model_class($result->Permission->toArray(), $this);
	}


	if (count($permissions) > 0)
	    return $this->getModelCollection($permissions);
	return array();

    }



    public function clearPermissionsForUser($pUserId){

	Doctrine_Query::create()
	    ->delete(FP_Application::getNamespace() . "_Dao_UserPermission o")
	    ->where("o.user_id = ?", $pUserId)
	    ->execute();
    }

    public function addPermissionToRole($pRoleId, $pPermission){

	if ($pPermission instanceof FP_Permission)
	    $permission_id = $pPermission->permission_id;
	else if (is_numeric($pPermission))
	    $permission_id = $pPermission;
	else if (is_string($pPermission) && !is_numeric($pPermission)){
	    $permission = $this->fetchPermissionByName($pPermission);
	    if ($permission)
		$permission_id = $permission->permission_id;
	    else
		throw new FP_Acl_Exception('Invalid permission');
	} else if (is_array($pPermission)){
	    $permission = $this->create($pPermission, true);
	    $permission_id = $permission->permission_id;
	} else
	    throw new FP_Acl_Exception('Invalid permission');

	$lookup = FP_Application::classFactory('Dao_RolePermission');
	$lookup->role_id = $pRoleId;
	$lookup->permission_id = $permission_id;
	$lookup->save();
    }

    public function addPermissionToUser($pUserId, $pPermission){

	if ($pPermission instanceof FP_Permission)
	    $permission_id = $pPermission->permission_id;
	else if (is_numeric($pPermission))
	    $permission_id = $pPermission;
	else if (is_string($pPermission) && !is_numeric($pPermission)){
	    $permission = $this->fetchPermissionByName($pPermission);
	    if ($permission)
		$permission_id = $permission->permission_id;
	    else
		throw new FP_Acl_Exception('Invalid permission');
	} else if (is_array($pPermission)){
	    $permission = $this->create($pPermission, true);
	    $permission_id = $permission->permission_id;
	} else
	    throw new FP_Acl_Exception('Invalid permission');

	$lookup = FP_Application::classFactory('Dao_UserPermission');
	$lookup->user_id = $pUserId;
	$lookup->permission_id = $permission_id;
	$lookup->save();
    }

    public function postDelete($pPermissionId){
	Doctrine_Query::create()
	    ->delete(FP_Application::getNamespace() . "_Dao_RolePermission rp")
	    ->where("rp.permission_id = ?", $pPermissionId)
	    ->execute();

	Doctrine_Query::create()
	    ->delete(FP_Application::getNamespace() . "_Dao_UserPermission up")
	    ->where("up.permission_id = ?", $pPermissionId)
	    ->execute();
    }
}
?>